package com.template.app.configuration;

import com.template.app.resolver.DatatableMethodArgumentResolver;
import org.apache.catalina.connector.Connector;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.EmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.MultipartConfigFactory;
import org.springframework.boot.context.embedded.ServletContextInitializer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.servlet.MultipartConfigElement;
import java.util.List;


/**
 * Created by shaoaq on 13-12-10.
 */
@Configuration
@EnableAutoConfiguration
@ComponentScan("com.yslc.erp.manage")
//@Import(OsfcConfiguration.class)
public class ConsoleConfiguration extends WebMvcConfigurerAdapter{

//    @Value("${logback_access_file}")
//    private String logbackAccessFile;
    public static final int MEMCACHE_PROTOCOL_BINARY = 1;
//    @Value("${static_file_path}")
//    private String staticFilePath;


    @Bean
    public EmbeddedServletContainerFactory servletContainer(){

        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(){
            @Override
            public EmbeddedServletContainer getEmbeddedServletContainer(ServletContextInitializer... initializers) {
                TomcatEmbeddedServletContainer embeddedServletContainer = (TomcatEmbeddedServletContainer) super.getEmbeddedServletContainer(initializers);
                Connector connector = embeddedServletContainer.getTomcat().getConnector();
                connector.setURIEncoding("UTF-8");

                return embeddedServletContainer;
            }
        };
//        LogbackValve valve = new LogbackValve();
////        valve.setFilename(logbackAccessFile);
//        factory.addContextValves(valve);
//        factory.setSessionTimeout(24, TimeUnit.HOURS);


        return factory;
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new DatatableMethodArgumentResolver());
        super.addArgumentResolvers(argumentResolvers);
    }

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize("10MB");
        factory.setMaxRequestSize("10MB");
        return factory.createMultipartConfig();
    }

//    @Bean
//    public FilterRegistrationBean loginFilter() {
//        LoginFilter loginFilter = new LoginFilter();
//        FilterRegistrationBean filterRegist = new FilterRegistrationBean();
//        filterRegist.setFilter(loginFilter);
//        filterRegist.addInitParameter("excludePatterns", ",user/login,logout,login,login.html,mixdsp.js,hl.html,regedit,regedit_a.html,regedit_c.html,industryCategory,contextCategory,check,byType,createcode,vcode,first,sec,upload,monitor,result.html,aptk.html,cptk.html,temp/addParticipants,addParticipants,temp/viewstat,viewstat,imageUpload,imageView");
//        filterRegist.addInitParameter("excludeType", "js,css,png,jpg,gif,jpeg");
//        filterRegist.addInitParameter("loginPage", "/login.html");
//        filterRegist.addUrlPatterns("/*");
//        return filterRegist;
//    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }

}
